//开机所有段码显示闪三次
//RTD_Settings 开机后一直闪耀, 按下旋钮按键后, 显示实际温度并开始温控
//按下旋钮按键后, 显示所有设定值, 从RTD_Settings开始闪耀,没任何操作,闪5次后隐藏

//闪耀,Settings更改后延迟1s闪耀
//WIFI未连接闪耀显示


#include <stdint.h>
#include "UI.h"
#include "GN1621_Parts.h"
#include "GN1621_Parts_Num.h"
#include "GN1621_Pin.h"


#define UI_Shine_Count 5 //正常闪耀4次

UI_System_S UI_System;


//UI_ON: 非熄火状态 且 RTD正常
UI_State_E UI_Grill_Normal(void)
{
    if((UI_System.Grill_state != State_Grill_Misfire) && (RTD[ADC_Oven].status == Pt_Normal)) return UI_ON;
    else return UI_OFF;
}

void UI_TempCtrl_Start(void)
{
    FanCtrlStart();
    UI_System.State_TempCtrl = UI_ON;
    UI_System.Grill_state = State_Grill_Ignite;
}
void UI_TempCtrl_Stop(void)
{
    if(UI_System.Grill_state == State_Grill_MisfireCountdown) return;

    if(UI_System.State_TempCtrl == UI_ON)
    {
        UI_System.State_TempCtrl = UI_OFF;
        FanCtrlStop();
        UI_System.Grill_state = State_Grill_MisfireCountdown;
    }
    else UI_System.Grill_state = State_Grill_Misfire;
}

void UI_Shine_Reset(void)
{
    UI_System.Shine_Pause = 2;
    UI_System.Shine_End = 10;
}

void UI_Encoder_KeyLong(void)
{
    Buzzer_Once();
    // if(UI_System.Grill_state == State_Grill_Idle) return;
    // if(UI_System.Grill_state != State_Grill_Misfire) UI_TempCtrl_Stop();

    TuYa_WiFi_ConfigNet();
}
void UI_Encoder_Key(void)
{
    // { //熄火状态,且倒计时归0后, 触发芯片复位
    //     UI_System.Grill_state = State_Grill_SetTemp;
    //     //关机状态重启
    //     UI_System.Is_Reset = UI_ON;
    //     UI_Init();
    //     GN1621_ON();
    //     return;
    // }
    Buzzer_Once();
    if(UI_System.Grill_state == State_Grill_Idle) return;

    if(UI_System.Grill_state == State_Grill_SetTemp)
    {
        UI_TempCtrl_Start(); 
        UI_System.Grill_state = State_Grill_Ignite;
        UI_System.RTD.Settings = UI_System.RTD.Settings_Modifiing;

        ZHT104C_Start();
    }
    else
    {
        if(UI_System.State_Operation == UI_None)
        {
            UI_System.State_Operation = UI_RTDSettings;
            UI_System.RTD.Settings_Modifiing = UI_System.RTD.Settings;
            UI_System.ProbeA.Settings_Modifiing = UI_System.ProbeA.Settings;
        }
        else if(UI_System.State_Operation == UI_RTDSettings) UI_System.State_Operation = UI_ProbeASettings;
        else if(UI_System.State_Operation == UI_ProbeASettings)
        {
            UI_System.State_Operation = UI_None;

            Setting_Exit();
        }
        UI_Shine_Reset();
        // if(UI_System.RTD.Settings_Modifiing != 0)
        // {
        //     UI_System.RTD.Settings = UI_System.RTD.Settings_Modifiing;
        //     UI_System.RTD.Settings_Modifiing = 0;
        // }
        // UI_System.State_Operation = UI_None;
        // UI_System.RTD.IsChanged = 0;
        ADC_KeyUpDown();
    }
}
void UI_Encoder_R(void)
{
    uint16_t temp = 0;

    Buzzer_Once();
    if(UI_System.Grill_state == State_Grill_Idle) return;
    if(UI_System.Grill_state == State_Grill_Misfire) return;

    if((UI_System.State_Operation == UI_RTDSettings) || (UI_System.Grill_state == State_Grill_SetTemp))
    { 
        if(UI_System.RTD.Settings_Modifiing == 0) UI_System.RTD.Settings_Modifiing = UI_System.RTD.Settings;
        temp = Oven_Setting_Max_F;
        if(UI_System.RTD.Settings_Modifiing < temp)
        {
            UI_System.RTD.Settings_Modifiing += 5;
            UI_System.RTD.IsChanged = 1;
        }
    }
    else if(UI_System.State_Operation == UI_ProbeASettings)
    {
        if(UI_System.ProbeA.Settings_Modifiing == 0) UI_System.ProbeA.Settings_Modifiing = UI_System.RTD.Settings;
        temp = Probe_Setting_Max_F;
        if(UI_System.ProbeA.Settings_Modifiing < temp)
        {
            UI_System.ProbeA.Settings_Modifiing += 5;
            UI_System.ProbeA.IsChanged = 1;
        }
    }

    UI_Shine_Reset();
}
void UI_Encoder_L(void)
{
    uint16_t temp = 0;

    Buzzer_Once();
    if(UI_System.Grill_state == State_Grill_Idle) return;
    if(UI_System.Grill_state == State_Grill_Misfire) return;

    if((UI_System.State_Operation == UI_RTDSettings) || (UI_System.Grill_state == State_Grill_SetTemp))
    {
        if(UI_System.RTD.Settings_Modifiing == 0) UI_System.RTD.Settings_Modifiing = UI_System.RTD.Settings;
        temp = Oven_Setting_Min_F;
        if(UI_System.RTD.Settings_Modifiing > temp)
        {
            UI_System.RTD.Settings_Modifiing -= 5;
            UI_System.RTD.IsChanged = 1;
        }
    }
    else if(UI_System.State_Operation == UI_ProbeASettings)
    {
        if(UI_System.ProbeA.Settings_Modifiing == 0) UI_System.ProbeA.Settings_Modifiing = UI_System.RTD.Settings;
        temp = Probe_Setting_Min_F;
        if(UI_System.ProbeA.Settings_Modifiing > temp)
        {
            UI_System.ProbeA.Settings_Modifiing -= 5;
            UI_System.ProbeA.IsChanged = 1;
        }
    }
    UI_Shine_Reset();
}

//F挡位转 C挡位
uint16_t Get_C_Gear(uint16_t F, uint16_t minCGear, uint16_t minFGear)
{
    float Seq = 0;
    float temp = 0;
    float tempMinF = 0;
    uint16_t newSeq = 0;
    uint16_t C = 0;

    temp = F;
    tempMinF = minFGear;
    Seq = (temp - tempMinF)/5.0;
    temp = (Seq / 1.8125); //直接丢弃小数,整数不加1
    newSeq = temp;
    C = minCGear + newSeq*5; //C挡位,增量5

    // printf("Get_C_Gear: %d %d %f %d %f %d\n",F, minFGear, Seq, newSeq, temp, C);
    return C;
}
//C挡位转 F挡位
uint16_t Get_F_Gear(uint16_t C, uint16_t minFGear, uint16_t minCGear)
{
    float Seq = 0;
    float temp = 0;
    float tempMinC = 0;
    uint16_t newSeq = 0;
    uint16_t F = 0;

    temp = C;
    tempMinC = minCGear;
    Seq = (temp - tempMinC) / 5.0;
    Seq = Seq*1.8125;
    newSeq = Seq; //丢弃小数，取整数
    temp = Seq - newSeq; //取小数
    if(temp > 0) newSeq += 1; //只要小数>0,整数+1
    F = newSeq * 5 + minFGear; //F挡位, 增量5

    // printf("Get_F_Gear: %d %d %f %d %f %d\n",C, minCGear, Seq, newSeq, temp, F);
    return F;
}

//程序烧录后，默认显示 F
void UI_Default_F(void)
{
    UI_System.Grill_Unit = Unit_F;
    UI_System.RTD.Settings = 350;
    UI_System.RTD.Settings_Modifiing = UI_System.RTD.Settings;

    UI_System.ProbeA.Settings = Probe_Setting_Max_F;
    UI_System.ProbeA.Settings_Modifiing = UI_System.ProbeA.Settings;
}
//程序烧录后，默认显示 C
void UI_Default_C(void)
{
    UI_System.Grill_Unit = Unit_C;

    UI_System.RTD.Settings = 350;
    UI_System.RTD.Settings_Modifiing = UI_System.RTD.Settings;

    UI_System.ProbeA.Settings = Probe_Setting_Max_F;
    UI_System.ProbeA.Settings_Modifiing = UI_System.ProbeA.Settings;

}

void UI_Init(void)
{
    UI_System.State_TempCtrl = UI_OFF;
    UI_System.ICO_WIFI = UI_OFF;
    UI_System.ICO_Bluetooth = UI_ON;
    UI_System.ICO_Auger = UI_OFF;
    UI_System.ICO_Fan = UI_OFF;
    UI_System.ICO_Heating = UI_OFF;
    UI_System.ICO_Error = UI_OFF;
    UI_System.ICO_Error_Auger = UI_OFF;
    UI_System.ICO_Error_Fan = UI_OFF;
    UI_System.ICO_Error_Ignite = UI_OFF;
    UI_System.ICO_Cell = UI_OFF;
    UI_System.Grill_state = State_Grill_SetTemp;
    UI_System.State_Operation = UI_None;
    UI_System.Shine_Pause = 0;
    UI_System.Shine_End = 0;

    UI_System.Alarm_Timer = Alarm_None;
    UI_System.Alarm_RTD = Alarm_None;
    UI_System.Alarm_ProbeA = Alarm_None;
    UI_System.Alarm_ProbeB = Alarm_None;
    
    UI_Default_F();

    UI_System.PowerUp_Delay = 300;

    Flash_Read();
}
void UI_Main(void)
{
    extern volatile unsigned int firmware_number; //固件版本号
    if(temp_set != UI_System.RTD.Settings) temp_set = UI_System.RTD.Settings;
    Lcd_Parts_Main();
    if(UI_System.RTD.Actual < UI_System.RTD.Settings)
    {
        if(UI_System.Grill_state == State_Grill_Ignite) UI_System.Grill_state = State_Grill_PreHeat;
    }
    else
    {
        if((UI_System.Grill_state == State_Grill_PreHeat) || (UI_System.Grill_state == State_Grill_Ignite)) UI_System.Grill_state = State_Grill_Cook;
    } 
    if(UI_System.Grill_state == State_Grill_Misfire)
    { //熄火状态 Clearn the all of alarm flag
        UI_System.Alarm_Timer = Alarm_None;
        UI_System.Alarm_RTD = Alarm_None;
        UI_System.Alarm_ProbeA = Alarm_None;
        UI_System.Alarm_ProbeB = Alarm_None;
    }
    if(UI_System.Alarm_Timer == Alarm_Timer1)
    {
        Buzzer_Alarm();
        UI_System.Alarm_Timer = Alarm_None;
    }
}

void Setting_Exit_RTD()
{
    if(UI_System.RTD.Settings_Modifiing != 0)
    {
        UI_System.RTD.Settings = UI_System.RTD.Settings_Modifiing;
        UI_System.RTD.Settings_Modifiing = 0;
    }
    UI_System.RTD.IsChanged = 0;
}
void Setting_Exit()
{
    Setting_Exit_RTD();
    if(UI_System.ProbeA.Settings_Modifiing != 0)
    {
        UI_System.ProbeA.Settings = UI_System.ProbeA.Settings_Modifiing;
        UI_System.ProbeA.Settings_Modifiing = 0;
    }
    UI_System.ProbeA.IsChanged = 0;
    UI_System.State_Operation = UI_None;
    ADC_KeyUpDown(); 
}

//10ms
void UI_Clock(void)
{
    static uint8_t count = 0;

    if(UI_System.PowerUp_Delay > 0) UI_System.PowerUp_Delay--;
    count++;
    if(count > 40)
    { //400ms
        count = 0;
        if(UI_System.ICO_WIFI == UI_Shine)
        {
            if(UI_System.Shine_WIFI == Shine_ON) UI_System.Shine_WIFI = Shine_OFF;
            else UI_System.Shine_WIFI = Shine_ON;
        }
        if(UI_System.Shine_ProbeA_Actual != Shine_Normal)
        {
            if(UI_System.Shine_ProbeA_Actual == Shine_ON) UI_System.Shine_ProbeA_Actual = Shine_OFF;
            else UI_System.Shine_ProbeA_Actual = Shine_ON;
        }

        if(UI_System.ICO_Error_Auger == UI_ON)
        {
            if(UI_System.Shine_AugerErr == Shine_ON) UI_System.Shine_AugerErr = Shine_OFF;
            else UI_System.Shine_AugerErr = Shine_ON;
        }
        else UI_System.Shine_AugerErr = Shine_OFF;

        if(UI_System.ICO_Error_Fan == UI_ON)
        {
            if(UI_System.Shine_FanErr == Shine_ON) UI_System.Shine_FanErr = Shine_OFF;
            else UI_System.Shine_FanErr = Shine_ON;
        }
        else UI_System.Shine_FanErr = Shine_OFF;

        if(UI_System.ICO_Error_Ignite == UI_ON)
        {
            if(UI_System.Shine_IgnitorErr == Shine_ON) UI_System.Shine_IgnitorErr = Shine_OFF;
            else UI_System.Shine_IgnitorErr = Shine_ON;
        }
        else UI_System.Shine_IgnitorErr = Shine_OFF;

        if(UI_System.Shine_RTD_Setting == Shine_ON) UI_System.Shine_RTD_Setting = Shine_OFF;
        else UI_System.Shine_RTD_Setting = Shine_ON;

        if(UI_System.Shine_Pause > 0) 
		{
			UI_System.Shine_Pause--;
		}
        else if(UI_System.Shine_End > 0)
        {
            if(UI_System.State_Operation != UI_None)
            {
                UI_System.Shine_End--;

                if(UI_System.Shine_End == 0)
                {
                    UI_System.Shine_Pause = 0;
                    // UI_System.State_Operation = UI_None;

                    // UI_System.RTD.Settings_Modifiing = 0;
                    // UI_System.RTD.IsChanged = 0;
                    // ADC_KeyUpDown();

                    Setting_Exit();
                }
            } 
        } 
    }
}
